<template>
  <div class="register">
      <div class="title">注册页</div>
      <div class="block">
          <input type="text" v-model="formData.phone" placeholder="输入手机号">
      </div>
        <div class="block">
          <input type="password"  v-model="formData.pass" placeholder="输入密码">
      </div>
        <div class="block">
          <input type="password" v-model="formData.checkpass" placeholder="输入正确密码">
      </div>
        <div class="block">
          <input type="button" value="注册" @click="register">
      </div>
  </div>
</template>

<script>
import {user_register} from '../utils/api'
export default {
    data(){
        return{
            formData:{
                phone:'',
                pass:'',
                checkpass:'',
            }
        }
    },
    methods:{
        register(){
            if(/^1[3-9]\d{9}$/.test(this.formData.phone) == false){
                alert('请输入合法的手机号');
            }else if(!this.formData.pass || !this.formData.checkpass ){
                alert('请输入密码/确认密码');
            }else if(this.formData.pass !== this.formData.checkpass){
                alert('密码和确认密码不一致');
            }else{
                var newFormData = JSON.parse(JSON.stringify(this.formData));
                delete newFormData.checkpass;
                console.log(this.formData);
                

                user_register(newFormData).then((res)=>{
                    if(res.data.code == 200){
                        alert('注册成功!');
                        this.$router.push('/login');
                    }else{
                        alert(res.data.msg);
                    }
                })
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.register{
  margin: 50px 20px;
}
.register .title{
  font-weight: bold;
  text-align: center;
  line-height: 40px;
}
.register .block{
  margin: 20px 0;
}
.register .block input{
  height: 40px;
  border: 1px solid #ccc;
  border-radius: 20px;
  padding-left: 20px;
  box-sizing: border-box;
  outline: none;
  width: 100%;
}
</style>